﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.ChungTu;
using TGBTech.Core.Models.DanhMuc;
using TGBTech.Core.Models.Models.BaoCao;
using TGBTech.Core.Models.Models.BaoCao.BangTongHopCongNoModel;

namespace TGBTech.Core.Models.Services
{
    public class BangTongHopCongNoService
    {
        public BangTongHopCongNo createBangTongHopCongNo(DateTime startDate, DateTime endDate, String MaTK)
        {
            //init with MaTK
            BangTongHopCongNo bangcongno = new BangTongHopCongNo();
            MaTK = MaTK.Trim();
            bangcongno.MaTK = MaTK;
            //init dbcontext
            using (TGBTechDb dbContext = new TGBTechDb())
            {
                var listData = dbContext.Data.ToList();
                //init list model with all KH
                this._initListModel(ref bangcongno, dbContext.KhachHang.ToList());
                //query for MATK
                var searchMaTK = (from e in listData
                                 where e.NgayCT <= endDate &&(e.TaiKhoanNoId.StartsWith(MaTK) || e.TaiKhoanCoId.StartsWith(MaTK) )
                                 select e).ToList();
                //query for DauKY          
                var searchDauKy = (from e in searchMaTK
                                  where e.NgayCT < startDate
                                  select e).ToList();
                //calculate no-co
                this._calculateNoCo(searchDauKy, ref bangcongno, true);
                //query for TrongKy
                var searchTrongKy = (from e in searchMaTK
                                    where e.NgayCT >= startDate && e.NgayCT <= endDate
                                    select e).ToList();
                //calculate no-co
                this._calculateNoCo(searchTrongKy, ref bangcongno, false);
                //calculate no-co CuoiKy
                this._calculateFinalNoCo(ref bangcongno);
            }
            return bangcongno;
        }

        private void _calculateFinalNoCo(ref BangTongHopCongNo bangcongno)
        {
            decimal nocuoiky;
            foreach (var model in bangcongno.ListModel)
            {
                nocuoiky = (model.NoDauKy + model.NoPhatSinh) - (model.CoDauKy + model.CoPhatSinh);
                if (nocuoiky < 0)
                {
                    model.CoCuoiKy = -nocuoiky;
                    model.NoCuoiKy = 0;
                }
                else
                {
                    model.NoCuoiKy = nocuoiky;
                    model.CoCuoiKy = 0;
                }
            }
        }

        private void _calculateNoCo(IEnumerable<Data> query, ref BangTongHopCongNo bangcongno, bool isDauKy)
        {
            decimal sumOfNo, sumOfCo;
            String matk = bangcongno.MaTK;
            IEnumerable<Data> queryKH;
            foreach (var model in bangcongno.ListModel)
            {
                //query for khachhang
                queryKH = (from e in query
                          where e.KhachHangId.StartsWith(model.MaKH)
                          select e).ToList();
                sumOfNo = (from e in queryKH
                           where e.TaiKhoanNoId.StartsWith(matk)
                           select e.SoTien).ToList().Sum();
                sumOfCo = (from e in queryKH
                           where e.TaiKhoanCoId.StartsWith(matk)
                           select e.SoTien).ToList().Sum();
                if (isDauKy)
                {
                    model.NoDauKy = sumOfNo;
                    model.CoDauKy = sumOfCo;
                }
                else
                {
                    model.NoPhatSinh = sumOfNo;
                    model.CoPhatSinh = sumOfCo;
                }
            }
        }

        private void _initListModel(ref BangTongHopCongNo bangcongno, List<KhachHang> khachhangs)
        {
            //check listmodel
            if (bangcongno.ListModel == null)
            {
                bangcongno.ListModel = new List<BangTongHopCongNoModel>();
            }
            //check khachhangs with null
            if (khachhangs == null || khachhangs.Count == 0)
            {
                return;
            }
            //init model for loop
            BangTongHopCongNoModel model;
            //loop each khachhang to create model
            foreach (var khachhang in khachhangs)
            {
                model = new BangTongHopCongNoModel()
                {
                    MaKH = khachhang.MaKH,
                    TenKH = khachhang.Name
                };
                //add model to list 
                bangcongno.ListModel.Add(model);
            }
        }
    }
}
